package com.lw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.mapper.PermissionApiMapper;
import com.lw.mapper.PermissionMapper;
import com.lw.mapper.PermissionMenuMapper;
import com.lw.mapper.PermissionPointMapper;
import com.lw.service.PermissionService;
import com.lw.system.Permission;
import com.lw.system.PermissionApi;
import com.lw.system.PermissionMenu;
import com.lw.system.PermissionPoint;
import com.lw.utils.BeanMapUtils;
import com.lw.utils.IdWorker;
import com.lw.utils.PermissionConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionMenuMapper permissionMenuMapper;
    @Autowired
    private PermissionApiMapper permissionApiMapper;
    @Autowired
    private PermissionPointMapper permissionPointMapper;
    @Autowired
    private IdWorker idWorker;
    @Override
    public void save(Map<String, Object> map) throws Exception {
        //设置主键
        String id=idWorker.nextId()+"";
        Permission perm= BeanMapUtils.mapToBean( map,Permission.class );
        perm.setId( id );
        //根据不同类型构造不同的资源对象
        switch (perm.getType()){
            case PermissionConstants.PY_MENU:
                PermissionMenu menu = BeanMapUtils.mapToBean( map, PermissionMenu.class );
                menu.setId( id );
                permissionMenuMapper.insert( menu );
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint point = BeanMapUtils.mapToBean( map, PermissionPoint.class );
                point.setId( id );
                permissionPointMapper.insert( point );
                break;
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean( map, PermissionApi.class );
                api.setId( id );
                permissionApiMapper.insert( api );
                break;
        }
        permissionMapper.insert( perm );
    }

    @Override
    public void update(Map<String, Object> map) throws Exception {
        Permission perm= BeanMapUtils.mapToBean( map,Permission.class );
        //先查后更
        Permission permission = permissionMapper.selectById( perm.getId() );
        permission.setCode( perm.getCode() );
        permission.setName( perm.getName() );
        permission.setDescription(perm.getDescription());
        permission.setEnVisible( perm.getEnVisible() );
        permissionMapper.updateById( permission );
        switch (perm.getType()){
            case PermissionConstants.PY_MENU:
                PermissionMenu menu = BeanMapUtils.mapToBean( map, PermissionMenu.class );
                permissionMenuMapper.updateById( menu );
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint point = BeanMapUtils.mapToBean( map, PermissionPoint.class );
                permissionPointMapper.updateById( point );
                break;
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean( map, PermissionApi.class );
                permissionApiMapper.updateById( api );
                break;
        }
    }

    @Override
    public List<Permission> findAll(Map<String, Object> map) {
        /*不传默认查询全部 查询permission一张表
         * type o 查询菜单和按钮 1菜单 2按钮 3api
         * enVisible 0查询Saas平台最高权限，1查询企业权限
         * pid 根据pid查询
         */
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if ("0".equals( map.get( "type" ) )){
           wrapper.eq( "type", PermissionConstants.PY_MENU).or().eq( "type",PermissionConstants.PY_POINT );
       }
        if (map.get( "type" )!=null){
            wrapper.eq( "type", map.get( "type" ));
        }
        if ( map.get( "enVisible" )!=null){
            wrapper.eq( "en_visible",map.get( "enVisible" ) );
        }
        if (map.get( "pid" )!=null){
            wrapper.eq( "pid",map.get( "pid" ) );
        }
        List<Permission> permissions = permissionMapper.selectList( wrapper );
        return permissions;
    }

    @Override
    public Map<String, Object> findById(String id) {
        Permission permission = permissionMapper.selectById( id );
        Object cPerm=null;
        if (permission.getType()==1){
            cPerm=permissionMenuMapper.selectById( id );
        }
        if (permission.getType()==2){
            cPerm=permissionPointMapper.selectById( id );
        }
        if (permission.getType()==3){
            cPerm=permissionApiMapper.selectById( id );
        }
        Map<String, Object> permissionMap = BeanMapUtils.beanToMap( permission );
        Map<String, Object> cPermMap = BeanMapUtils.beanToMap( cPerm );
        permissionMap.putAll( cPermMap );
        return permissionMap;
    }

    @Override
    public void delete(Map<String, Object> map) {
        Permission permission = permissionMapper.selectById( (String) map.get( "id" ) );
        if (permission.getType()==1){
            permissionMenuMapper.deleteById( (String) map.get( "id" ) );
        }
        if (permission.getType()==2){
            permissionPointMapper.deleteById( (String) map.get( "id" ) );
        }
        if (permission.getType()==3){
            permissionApiMapper.deleteById( (String) map.get( "id" ) );
        }
        permissionMapper.deleteById( (String) map.get( "id" ) );
    }
}
